package org.service.car.activity;

import java.sql.SQLException;
import java.util.List;

import org.service.car.R;
import org.service.car.database.Util;
import org.service.car.helper.MyExpandableAdapter;
import org.service.car.helper.Preferencia;
import org.service.car.pojo.Revisao;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ConsultaRevisoes extends Activity {

	private ExpandableListView expList;
	private MyExpandableAdapter adapter;
	private TextView lblVeiculoEscolhido;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_expandivel);

		buscarControles();
		setarValorVeiculoEscolhido();
		adapter = new MyExpandableAdapter(this, montarDataSetRevisao());
		expList.setAdapter(adapter);

	}

	@Override
	protected void onStop() {
		super.onStop();
		Util.fecharConexao(getApplicationContext());
		System.gc();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Util.fecharConexao(getApplicationContext());
		System.gc();
	}

	private void buscarControles() {
		expList = (ExpandableListView) findViewById(R.id.listExpandivel);
		lblVeiculoEscolhido = (TextView) findViewById(R.id.lblveiculoEscolhido);
	}

	private void setarValorVeiculoEscolhido() {
		lblVeiculoEscolhido.setText(Preferencia.obterPreferencia(
				getApplicationContext(), "Veiculo", ""));
	}

	private List<Revisao> montarDataSetRevisao() {
		List<Revisao> listaRevisoes = null;
		try {
			listaRevisoes = Util.getRevisoesPorVeiculo(getApplicationContext(),
					lblVeiculoEscolhido.getText().toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listaRevisoes;
	}

}
